<?php

/**
 * api actions.
 *
 * @package    newproject
 * @subpackage api
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class userActions extends sfActions
{

	public function preExecute(){
		$this->setTemplate('result');
	}

	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeGet(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$this->forward404Unless($userid);
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$result->setData('user',$user->toJSONArray());
			}else{
				$result->setErrorMessage("User not found");
			}
		}catch (Exception $e){
			$result->setMessage($e->getMessage());
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}

	public function executeCreate(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$this->forward404Unless($userid);
		try {
			/* var $user User*/
			$user = UserContainerPeer::createNewUserFromRequest($userid,$request);
			if($user){
				$result->setData('user',$user->toJSONArray());
			}
		} catch (LypoUserException $e){
			$result->setData('user',$e->getUser()->toJSONArray());
			$result->setErrorMessage($e->getMessage());
		} catch (Exception $e){
			$result->setErrorMessage($e->getMessage());
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$this->forward404Unless($userid);
		try {
			/* var $user User*/
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$user->updateFromWebRequest($request);
				$user->save();
				$result->setData('user',$user->toJSONArray());
			}else{
				$result->setErrorMessage("User not found");
			}
		} catch (Exception $e){
			$result->setMessage($e->getMessage());
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}

	/*public function executeDelete(sfWebRequest $request)
	 {
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$this->forward404Unless($userid);
		try {
		$user = UserPeer::getByContainerString($userid);
		if($user){
		$user->delete();
		$result->setData('user',$user->toJSONArray());
		}else{
		$result->setErrorMessage("User not found");
		}
		} catch (Exception $e){
		$result->setErrorMessage($e->getMessage());
		}
		$this->result = $result;
		}*/

	public function executeOpposites(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$this->forward404Unless($userid);
		try {
			/* var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$opposites = $user->getOpposites(true);
				$result->setData('opposites',$user->toJSONArray());
			}else{
				$result->setErrorMessage("User not found");
			}
		}catch (Exception $e){
			$result->setMessage($e->getMessage());
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}


	public function executeAddopposite(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$opposite_id = $request->getParameter('opposite_id',false);
		$this->forward404Unless($userid);
		try {
			/* var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$user->addPerfectOpposite($opposite_id);
			}else{
				$result->setErrorMessage("User or opposite not found");
			}
		}catch (Exception $e){
			$result->setMessage($e->getMessage());
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}

	public function executeRemoveopposite(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$opposite_id = $request->getParameter('opposite_id',false);
		$this->forward404Unless($userid);
		try {
			/* var $user User */
			$opposite = UserPeer::retrieveByPK($opposite_id);
			$user = UserPeer::getByContainerString($userid);
			if($user && $opposite){
				$user->removePerfectOpposite($opposite_id);
				$result->setMessage('Perfect opposite removed');
			}else{
				$result->setErrorMessage("User or opposite not found");
			}
		}catch (Exception $e){
			$result->setMessage($e->getMessage());
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}


	public function executeNetworkcode(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$this->forward404Unless($userid);
		try {
			/* var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$mergecode = UserNetworkCodePeer::generateNetworkCodeForUser($user);
				$result->setData('code',$mergecode->getCode());
			}else{
				$result->setErrorMessage("User not found");
			}
		}catch (Exception $e){
			$result->setMessage($e->getMessage());
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}


	public function executeNetworkmerge(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid_to_merge = $request->getParameter('userid');
		$code = $request->getParameter('code');
		$this->forward404Unless($userid_to_merge);
		try {
			if($userid_to_merge && $code){
				$to_user = UserNetworkCodePeer::getUserForCode($code);
				UserContainerPeer::mergeUsers($to_user,$userid_to_merge);
				//Code weghalen!
				$c = new Criteria();
				$c->add(UserNetworkCodePeer::CODE,$code);
				$c->add(UserNetworkCodePeer::USER_ID,$to_user->getId());
				UserNetworkCodePeer::doDelete($c);
			}else{
				$result->setErrorMessage("User and/or networkcode not found");
			}
		}catch (Exception $e){
			$result->setMessage($e->getMessage());
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}

	public function executeOauth(sfWebRequest $request)
	{

		try {
			$this->setLayout(false);
			
			//Initialize the new signature method with a factory method!
			$signature_method = OAuthSignatureFactory::createFromConsumerKey($request->getParameter('oauth_consumer_key'));

			//Build a request object from the current request	
			$oareq = OAuthRequest::from_request(null, null, array_merge($_GET, $_POST));
			
			//Check the request signature
			@$signature_valid = $signature_method->check_signature($oareq, null, null, $request->getParameter("oauth_signature"));
	
			//Build the output object
			$payload = array();
			if ($signature_valid == true) {
				$payload["validated"] = "Success! The data was validated";
			} else {
				$payload["validated"] = "This request was spoofed";
			}
	
			//Add extra parameters to help debugging
			$payload["query"] = array_merge($_GET, $_POST);
			$payload["rawpost"] = file_get_contents("php://input");
	
			//Return the response as JSON
			return $this->renderText(json_encode($payload));
		} catch (sfException $e){
			$result = new LypoResult();
			$result->setErrorMessage($e->getMessage());
			$this->setLayout('result');
			return $this->renderText($result->toJSON());
		}
	}

}
